package com.sino.androiddesign.design

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentPagerAdapter
import android.support.v7.app.ActionBarDrawerToggle
import android.view.Gravity
import android.widget.Toast
import com.sino.androiddesign.R
import com.sino.androiddesign.recyclerviewdemo.MultipleItemFragment
import kotlinx.android.synthetic.main.activity_tab.*

/**
 * 标签栏
 * 状态指示栏的颜色
 * app:tabIndicatorColor="@android:color/holo_orange_light"
 * 状态指示栏的高度
 * app:tabIndicatorHeight="2dp"
 * 滑动方式 scrollable 当多的时候可以滑动  fixed 固定等分
 * app:tabMode="scrollable"
 * 选中文字颜色
 * app:tabSelectedTextColor="@android:color/holo_orange_light"
 * 未选中时候的颜色
 * app:tabTextColor="@android:color/holo_red_dark"
 */
class TabActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_tab)

        var titles: Array<String> = arrayOf("标签一", "标签二", "标签三", "标签四", "标签五")
        var frags = ArrayList<Fragment>();
        for (i in 0 until titles.size) {
            frags.add(MultipleItemFragment())
        }

        tab_vp.adapter = TabAdapter(supportFragmentManager, titles, frags)
        tab_tab1.setupWithViewPager(tab_vp)
        // 如果需要用到自定义view可以使用 TabLayout.Tab
//        tab_tab1.addTab(tab_tab1.newTab())


        /**
         * 抽屉式布局的使用
         */
        setSupportActionBar(tab_toolbar)
        var actionBarDrawerToggle = ActionBarDrawerToggle(this, tab_drawer, tab_toolbar, R.string.app_name, R.string.app_name)
        actionBarDrawerToggle.syncState()
        tab_drawer.addDrawerListener(actionBarDrawerToggle)
       tab_navigation.setNavigationItemSelectedListener {

           true
       }
        tab_toolbar.setNavigationOnClickListener {
            if (tab_drawer.isDrawerOpen(Gravity.START)){
                tab_drawer.closeDrawer(Gravity.START)
            }else{
                tab_drawer.openDrawer(Gravity.START)

            }
        }
    }
}

class TabAdapter(fm: FragmentManager?, var titles: Array<String>, var frags: ArrayList<Fragment>) : FragmentPagerAdapter(fm) {

    override fun getItem(position: Int): Fragment {
        return frags[position]
    }

    override fun getCount(): Int {
        return titles.size
    }

    override fun getPageTitle(position: Int): CharSequence? {
        return titles[position]
    }
}
